package Yki.PaperPlaneClient.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;

/**
 * 选择日期的对话框
 * 
 * @author 卢志泳
 *
 */
public class TimeChooseDialog extends Dialog {
	private LinearLayout layout;
	private DatePicker datePicker;
	private Button okButton;
	
	//年月日
	private int year;
	private int month;
	private int day;
	
	/**
	 * 
	 * @param context
	 * @param onEnsureListener	传入的Handler对象，用于返回具体时间
	 */
	public TimeChooseDialog(final Context context, final Handler onEnsureListener) {
		super(context);
		// TODO Auto-generated constructor stub
		setTitle("请选择时间");
		
		layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		datePicker = new DatePicker(context);
		okButton = new Button(context);
		okButton.setText("OK");
		okButton.setTextSize(25);
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER;
		
		layout.addView(datePicker, params);
		layout.addView(okButton, params);
		
		setContentView(layout);
		
		//时间的初始值为当天
		Time time = new Time();
		time.setToNow();
		year = time.year;
		month = time.month;
		day = time.monthDay;
		datePicker.init(time.year, time.month, time.monthDay, new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				year = arg1;
				month = arg2;
				day = arg3;
			}
		});
		
		//按下OK后发送消息
		okButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Time time = new Time();
				time.year = year;
				time.month = month;
				time.monthDay = day;
				
				Message msg = new Message();
				msg.what = 0;
				msg.obj = time;
				onEnsureListener.sendMessage(msg);
				
				cancel();
			}
		});
	}
}